/*
 * Copyright (c) 2025. hallele, Inc All rights reserved.
 */

package cn.hallele.ms.portalcenter.das;

import cn.hallele.infrastructure.metadata.io.Page;
import cn.hallele.infrastructure.util.ObjectUtil;
import cn.hallele.mc.sdk.portalcenter.dto.DataDictTypeDTO;
import cn.hallele.mc.sdk.portalcenter.dto.DataDictTypeQueryDTO;
import cn.hallele.ms.infrastructure.core.convertor.das.ModelDas;
import cn.hallele.ms.portalcenter.application.bo.DataDictTypeBO;
import cn.hallele.ms.portalcenter.application.bo.DataDictTypeQueryBO;
import cn.hallele.ms.starter.web.converter.YesOrNoEnum;
import cn.hallele.ms.starter.web.framework.ActionExecutor;
import cn.hallele.ms.starter.web.framework.context.ActionContext;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * hallele_ms_configcenter
 *
 * @author anle5
 * @since 2025/1/10 23:26
 */
@Slf4j
@Component
public class DataDictTypeDBDas extends ModelDas<ActionExecutor, DataDictTypeDTO, DataDictTypeBO> {
    protected DataDictTypeDBDas(ActionExecutor option) {
        super(option);
    }

    private static final String QUERY_DATA_DICT_TYPE_LIST = "queryDataDictTypeListApplicationService";

    /**
     * 获取 数据字典列表
     *
     * @param condition 查询条件
     * @return List
     */
    public Page<DataDictTypeDTO> getDataDictTypes(@Valid DataDictTypeQueryDTO condition) {
        ActionContext context = new ActionContext();
        this.option.execute2Resp(QUERY_DATA_DICT_TYPE_LIST, convertQuery(condition), context);
        Page<DataDictTypeBO> result = context.getResult();
        return revertPage(result);
    }

    /**
     * DataDictTypeQueryDTO 转 DataDictTypeQueryBO
     *
     * @param condition MenuQueryDTO
     * @return MenuQueryBO
     */
    private DataDictTypeQueryBO convertQuery(DataDictTypeQueryDTO condition) {
        return ObjectUtil.map(condition, d -> DataDictTypeQueryBO.builder()
                .keywords(d.getKeywords())
                .startTime(d.getStartTime())
                .endTime(d.getEndTime())
                .pageNo(d.getPageNo())
                .pageSize(d.getPageSize())
                .name(d.getName())
                .code(d.getCode())
                .status(ObjectUtil.map(d.getStatus(), YesOrNoEnum::valueOfCode))
                .build());
    }

    /**
     * 将T转成E
     *
     * @param dataDictTypeDTO 被转换者
     * @return S 转换后者
     */
    @Override
    protected DataDictTypeBO convert(DataDictTypeDTO dataDictTypeDTO) {
        return ObjectUtil.map(dataDictTypeDTO, d -> DataDictTypeBO.builder()
                .id(d.getId())
                .code(d.getCode())
                .name(d.getName())
                .status(ObjectUtil.map(d.getStatus(), YesOrNoEnum::valueOfCode))
                .description(d.getDescription())
                .sort(d.getSort())
                .createBy(d.getCreateBy())
                .updateBy(d.getUpdateBy())
                .createTime(d.getCreateTime())
                .updateTime(d.getUpdateTime())
                .build());
    }

    /**
     * 将E回转陈T
     *
     * @param dataDictTypeBO 被回转换者
     * @return S 回转换后者
     */
    @Override
    protected DataDictTypeDTO revert(DataDictTypeBO dataDictTypeBO) {
        return ObjectUtil.map(dataDictTypeBO, d -> DataDictTypeDTO.builder()
                .id(d.getId())
                .code(d.getCode())
                .name(d.getName())
                .status(ObjectUtil.map(d.getStatus(), YesOrNoEnum::getCode))
                .description(d.getDescription())
                .sort(d.getSort())
                .createBy(d.getCreateBy())
                .updateBy(d.getUpdateBy())
                .createTime(d.getCreateTime())
                .updateTime(d.getUpdateTime())
                .build());
    }
}
